local lsp_hengyan = fk.CreateSkill {
    name = "lsp_hengyan",
    anim_type = "masochism",
}

Fk:loadTranslationTable {
    ["lsp_hengyan"] = "烆焱",
    [":lsp_hengyan"] = "结束阶段，你可以失去至多体力值的盈能并选择一名其他角色，将至多等量张牌置于其武将牌旁，并获得其等量的牌，若不足，你可以继续获得其相邻角色的牌，若依旧不足或点击取消视为对其使用剩余不足数量的【杀】，结算结束后，其获得因此置于武将牌旁的牌。"
}
lsp_hengyan:addEffect(fk.EventPhaseStart, {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and player.phase == Player.Finish and
            player:getMark("@lsp_lingying") > 0
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local _, dat = room:askToUseActiveSkill(player, {
            skill_name = "lsp_hengyan_active",
            prompt = "烆焱：请选择要失去的盈能数",
            cancelable = true,
        })
        if dat then
            event:setCostData(self, { choice = dat.interaction })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local n = event:getCostData(self).choice
        room:setPlayerMark(player, "lsp_hengyan_jiaohuan-phase", n)
        room:removePlayerMark(player, "@lsp_lingying", n)
        local id, cards = room:askToChooseCardsAndPlayers(player, {
            min_card_num = 1,
            max_card_num = n,
            min_num = 1,
            max_num = 1,
            targets = room:getOtherPlayers(player, false),
            skill_name = lsp_hengyan.name,
            prompt = "烆焱：你可以将至多" .. n .. "张牌置于一名其他角色的武将牌旁",
            cancelable = true,
            will_throw = false,
        })
        if #id > 0 and #cards > 0 then
            id[1]:addToPile("#lsp_hengyan", cards, true, lsp_hengyan.name)
            local slash = false
            if id[1]:getHandcardNum() < n then
                local getnum = id[1]:getHandcardNum()
                room:obtainCard(player, id[1]:getCardIds("h"), false, fk.ReasonPrey, player, lsp_hengyan.name)
                room:removePlayerMark(player, "lsp_hengyan_jiaohuan-phase", getnum)
                slash = true
            end
            if id[1]:getHandcardNum() == n then
                room:obtainCard(player, id[1]:getCardIds("h"), false, fk.ReasonPrey, player, lsp_hengyan.name)
            end
            if id[1]:getHandcardNum() > n then
                local tocards = room:askToChooseCards(player, {
                    min = n,
                    max = n,
                    target = id[1],
                    flag = "h",
                    skill_name = lsp_hengyan.name,
                    prompt = "烆焱：请选择你要获得的牌，当前待选：" .. n,
                })
                room:moveCardTo(tocards, Card.PlayerHand, player, fk.ReasonPrey, lsp_hengyan.name, nil, false, player)
            end
            if slash == true then
                local targets = table.filter(room:getOtherPlayers(player),
                    function(t) return (id[1]:getLastAlive() == t or t:getLastAlive() == id[1]) and not t:isKongcheng() end)
                if #targets == 0 then
                    for _ = 1, player:getMark("lsp_hengyan_jiaohuan-phase"), 1 do
                        room:useVirtualCard("slash", nil, player, id[1], lsp_hengyan.name, true)
                    end
                end
                local next = room:askToChoosePlayers(player, {
                    min_num = 1,
                    max_num = 1,
                    targets = targets,
                    skill_name = lsp_hengyan.name,
                    prompt = "烆焱：请选择要交给牌的角色，当前交换的数量：" ..
                        player:getMark("lsp_hengyan_jiaohuan-phase") ..
                        "点击取消视为" .. id[1].id .. "使用" .. player:getMark("lsp_hengyan_jiaohuan-phase") .. "张【杀】",
                    cancelable = true,
                })
                if #next > 0 then
                    if next[1]:getHandcardNum() < player:getMark("lsp_hengyan_jiaohuan-phase") then
                        room:obtainCard(player, next[1]:getCardIds("h"), false, fk.ReasonPrey, player, lsp_hengyan.name)
                        for _ = 1, player:getMark("lsp_hengyan_jiaohuan-phase"), 1 do
                            room:useVirtualCard("slash", nil, player, id[1], lsp_hengyan.name, true)
                        end
                    end
                    if next[1]:getHandcardNum() == n then
                        room:obtainCard(player, next[1]:getCardIds("h"), false, fk.ReasonPrey, player, lsp_hengyan.name)
                    end
                    if next[1]:getHandcardNum() > n then
                        local tocards = room:askToChooseCards(player, {
                            min = n,
                            max = n,
                            target = next[1],
                            flag = "h",
                            skill_name = lsp_hengyan.name,
                            prompt = "烆焱：请选择你要获得的牌，当前待选：" .. n,
                        })
                        room:moveCardTo(tocards, Card.PlayerHand, player, fk.ReasonPrey, lsp_hengyan.name, nil, false,
                            player)
                    end
                end
                if #next == 0 then
                    for _ = 1, player:getMark("lsp_hengyan_jiaohuan-phase"), 1 do
                        room:useVirtualCard("slash", nil, player, id[1], lsp_hengyan.name, true)
                    end
                end
            end
            if not id[1].dead then
                room:moveCards({ --移动私人牌堆案例
                    ids = id[1]:getPile("#lsp_hengyan"),
                    from = id[1],
                    fromArea = Card.PlayerSpecial,
                    to = id[1],
                    toArea = Card.PlayerHand,
                    moveReason = fk.ReasonJustMove,
                    skillName = lsp_hengyan.name,
                    specialName = "#lsp_hengyan",
                    moveVisible = false,
                })
            end
        end
        if player:getMark("@lsp_lingying") == 0 then
            local isDeputy = true
            if player.general == "lsp__liyue_ex" then
                isDeputy = false
            end
            room:changeHero(player, "lsp__liyue", false, isDeputy, true, false)
            room:setPlayerMark(player, "lsp_maoding_skill2-phase", 0)
            room:setPlayerMark(player, MarkEnum.SlashResidue .. "-phase", 0)
        end
    end,
})

return lsp_hengyan
